package com.example.wangyi.mvp.ui.fragment.home;

import android.graphics.Color;
import android.util.Log;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.alibaba.android.vlayout.DelegateAdapter;
import com.alibaba.android.vlayout.VirtualLayoutManager;
import com.alibaba.android.vlayout.layout.ColumnLayoutHelper;
import com.alibaba.android.vlayout.layout.GridLayoutHelper;
import com.alibaba.android.vlayout.layout.SingleLayoutHelper;
import com.example.wangyi.R;
import com.example.wangyi.base.BaseFragment;
import com.example.wangyi.base.BasePresenter;
import com.example.wangyi.mvp.model.bean.HomeBean;
import com.example.wangyi.mvp.presenter.HomePresenter;

import java.util.ArrayList;

import butterknife.BindView;


public class HomeFragment extends BaseFragment {

    private String TAG = HomeFragment.class.getCanonicalName();
    @BindView(R.id.home_tv_title)
    TextView homeTvTitle;
    @BindView(R.id.home_rv)
    RecyclerView homeRv;
    private ArrayList<HomeBean.DataDTO.BannerDTO> banner;
    private HomeSeekAdapter homeSeekAdapter;
    private VirtualLayoutManager layoutManager;
    private DelegateAdapter adapter;
    private HomeBannerAdapter homeBannerAdapter;
    private ArrayList<HomeBean.DataDTO.ChannelDTO> channel;
    private HomeBannerChannelAdapter homeBannerChannelAdapter;

    @Override
    protected BasePresenter createPresenter() {
        return new HomePresenter(this);
    }

    @Override
    protected void initData() {
        getmPresenter().start();

    }

    @Override
    protected void init() {
        homeTvTitle.setText(R.string.home_title);
        //公共布局管理器
        layoutManager = new VirtualLayoutManager(getContext());

        //数据集合
        banner = new ArrayList<>();
        channel = new ArrayList<>();

        //设置回收复用线程大小
        RecyclerView.RecycledViewPool recycledViewPool = new RecyclerView.RecycledViewPool();
        homeRv.setRecycledViewPool(recycledViewPool);
        recycledViewPool.setMaxRecycledViews(0, 15);

        //搜索
        homeSeekAdapter = initSeek();
        //Banner
        homeBannerAdapter = initBanner();
        //Banner下条目
        homeBannerChannelAdapter = initBannerChannel();


    }

    private HomeBannerChannelAdapter initBannerChannel() {
        ColumnLayoutHelper columnLayoutHelper = new ColumnLayoutHelper();
        columnLayoutHelper.setItemCount(channel.size());// 设置布局里Item个数
        columnLayoutHelper.setPadding(20, 50, 20, 0);
        columnLayoutHelper.setBgColor(Color.WHITE);// 设置背景颜色
        columnLayoutHelper.setAspectRatio(6);// 设置设置布局内每行布局的宽与高的比
        HomeBannerChannelAdapter gridAdapter = new HomeBannerChannelAdapter(columnLayoutHelper, channel);
        return gridAdapter;
    }

    private HomeBannerAdapter initBanner() {
        ColumnLayoutHelper columnLayoutHelper = new ColumnLayoutHelper();
        // 创建对象

        // 公共属性
        columnLayoutHelper.setItemCount(1);// 设置布局里Item个数
//        columnLayoutHelper.setBgColor(Color.GRAY);// 设置背景颜色
        columnLayoutHelper.setAspectRatio(2);// 设置设置布局内每行布局的宽与高的比
        HomeBannerAdapter geAdapter = new HomeBannerAdapter(columnLayoutHelper, banner);
        return geAdapter;
    }

    private HomeSeekAdapter initSeek() {
        SingleLayoutHelper singleLayoutHelper = new SingleLayoutHelper();
        singleLayoutHelper.setItemCount(1);//设置布局个数
        singleLayoutHelper.setBgColor(Color.GRAY);//设置布局背景颜色\
        singleLayoutHelper.setAspectRatio(6);//设置布局内每行的宽高
        //设置适配器
        HomeSeekAdapter homeSeekAdapter = new HomeSeekAdapter(singleLayoutHelper);
        //
        return homeSeekAdapter;
    }

    @Override
    public void onSuccess(Object o) {
        super.onSuccess(o);
        if (o instanceof HomeBean) {
            Log.e("TAG", "onSuccess: " + ((HomeBean) o).getData());
//            HomeAdapter homeAdapter = new HomeAdapter(((HomeBean) o).getData(),getActivity());
//            homeRv.setAdapter(homeAdapter);
//            homeAdapter.notifyDataSetChanged();
            HomeBean.DataDTO data = ((HomeBean) o).getData();
            //清空banner集合数据
            banner.clear();
            //添加banner数据
            banner.addAll(data.getBanner());
            //刷新banner适配器
            homeBannerAdapter.notifyDataSetChanged();
            //清空banner下图片集合数据
            channel.clear();
            //添加banner下图片数据
            channel.addAll(data.getChannel());
            //刷新banner下图片适配器
            homeBannerChannelAdapter.notifyDataSetChanged();

            adapter = new DelegateAdapter(layoutManager, false);
            adapter.addAdapter(homeSeekAdapter);
            adapter.addAdapter(homeBannerAdapter);
            adapter.addAdapter(homeBannerChannelAdapter);

        }
        //设置布局管理器 == 绑定适配器
        initAddAdapter();
    }

    private void initAddAdapter() {
        homeRv.setLayoutManager(layoutManager);
        homeRv.setAdapter(adapter);
    }

    @Override
    public void onError(String msg) {
        super.onError(msg);
        Log.e(TAG, "onError: " + msg);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_home;
    }
}